﻿<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class="ibox-content" id="toolbar">
        <form action="" class="form-inline" id="form" data-url="">
            <div class="form-group">
                <label class="control-label" for="key">状态:</label>
                <select id="key" class="form-control" name="key">
                    <option value="">请选择</option>
                    <option value="id">主键</option>
                    <option value="u_id">ID</option>
                    <option value="nickname">用户名</option>
                    <option value="family_name">家族名</option>
                </select>
            </div>

            <div class="form-group">
                <label class="control-label" for="value">输入:</label>
                <input class="form-control" id="value" type="text" name="value">
            </div>
            <button type="button" class="btn btn-primary" id="eventQuery">查询</button>
            <button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>
        </form>
        <div class="deal-btn">
            <button class="btn btn-w-m btn-success" id="batch-btn">批量禁封</button>
            <!--<button class="btn btn-w-m btn-warning" id="export-btn">导出数据</button>-->
        </div>
    </div>
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">用户列表</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <table id="tb_departments"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}
<script>
    $(document).ready(function () {
        initTable();
    });

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/CustomerIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'post',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showHeader: true,
            showToggle: true,
            showRefresh: true,
            sortStable: true,
            sortName: 'id',
            sortOrder: "desc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            minimumCountColumns: 2,             //最少允许的列数
            toolbar: '#toolbar',
            onPostBody:function () {
                //复原checkbox
                var allObj = $('#all');
                if(allObj.attr('checkedAttr') == 1){
                    $('input[name=item]').prop('checked',true);
                }
            },
            columns: [{
                field: 'CheckBox',
                title: "<input type='checkbox' value='全选' id='all' checkedAttr='0' onclick='allCheck()'/>",
                align: 'center',
                formatter: batchOperateFormatter
            },{
                field: 'id',
                title: '主键',
                align: 'center',
                sortable: true
            }, {
                field: 'u_id',
                title: 'ID',
                align: 'center',
                sortable: true,
                editable: {
                    type: 'number',
                    title: '更改用户信息（ID）'
                }
            }, {
                field: 'nickname',
                title: '用户名',
                align: 'center'
            }, {
                field: 'gold',
                title: '金币',
                align: 'center'
            }, {
                field: 'diamonds',
                title: '钻石',
                align: 'center'
            }, {
                field: 'money',
                title: '金钱',
                align: 'center'
            },{
                field: 'u_phone',
                title: '手机号',
                align: 'center',
                formatter: function (value, row, index) {

                    if(row.u_phone == null ){
                        return "无";
                    }else{
                        return row.u_phone;
                    }
                }
            }, {
                field: 'followers',
                title: '粉丝数',
                align: 'center',
            }, {
                field: 'friends',
                title: '关注数',
                align: 'center',
            }, {
                field: 'union_name',
                title: '家族名称',
                align: 'center',
                formatter:function (value, row, index) {
                    return row.union_name ? row.union_name : '暂无';
                }
            },{
                field: 'last_login',
                title: '上次登录时间',
                align: 'center',
            }, {
                field: 'create_time',
                title: '注册时间',
                align: 'center',
            }, {
                field: 'Button',
                title: '操作',
                events: operateEvents,
                formatter: operateFormatter
            }],
            //保存的使用
            onEditableSave: function (field, row, oldValue, $el) {
                //可进行异步操作
                layer.confirm('确认修改吗？，请先检查是否有重名',{
                    title:['提示','font-size:18px;font-family:宋体;font-weight:700;'],
                    btn: ['确定','取消'] //按钮
                },function(){
                    $.ajax({
                        type: "get",
                        url: "/UserEdit",
                        data:{
                            'user_id':row.id,
                            'u_id':row.u_id
                        } ,
                        dataType: 'JSON',
                        success: function (re) {
                            if (re.code === 100) {
                                layer.msg(re.msg,{
                                    icon: 1,
                                    time: 2000
                                });
                            } else {
                                layer.msg(re.msg,{
                                    icon:2,
                                    time:1000
                                });
                                $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                            }
                        },
                        error: function () {
                            alert('编辑失败');
                        }
                    });
                })
            }
        });
    }

    //得到查询的参数

    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        console.log(JSON.stringify(params));
        return params;
    }

    //批量操作
    function batchOperateFormatter(value,row,index){
        return [
            "<input type='checkbox' name='item' value='" + row.id + "'/>"
        ].join('');
    }

    var itemArr = [];
    function allCheck(){
        //因为bootstrap table的影响传统的获取checkbox状态方法均获取到一直为false，所以采取以下方案（同时bootstrap table不支持表标题行事件）
        var allObj = $('#all');
        if(allObj.attr('checkedAttr') == 0){
            $('input[name=item]').prop('checked',true);
            allObj.attr('checkedAttr',1);
        }
        else{
            $('input[name=item]').prop('checked',false);
            allObj.attr('checkedAttr',0);
        }
    }

    $('#batch-btn').click(function () {
        itemArr = [];
        //获取选择的申请单id数组
        $('input[name=item]:checked').each(function(){
            itemArr.push($(this).val());
        });
        if(itemArr.length == 0){
            layer.msg('请选择需要批量处理的申请单！',{
                time:1500
            });
            return false;
        }
        console.log(itemArr);
        layer.open({
            type: 2,
            title: ['禁封账号','font-size:18px;font-family:宋体;font-weight:700;'],
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['900px', '600px'],
            content:"/UserBan?user_id=" + itemArr + '&type=1',  //type=1 批量禁封 不是则单个禁封
            offset:'auto',
            scrollbar:false,
            end: function () {
                $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
            }
        });
    });

    function operateFormatter(value, row, index) {

        var status = row.status;
        var disabled_time = row.disabled_time;
        if(status === 1 && !disabled_time){
            var state = '禁封账号';
            var control = 'ban-btn';
            var color = 'btn-danger';
        }else if(status === 2 || disabled_time){
            var state = '解封账号';
            var control = 'rid-btn';
            var color = 'btn-primary';
        }



//        if(row.union_il){
//            var sh = 'none';
//        }else{
//            var sh = 'inline-block';
//        }

        if(row.device === 1){
            var dev = '解封设备';
            var dev_color = 'btn-primary';
            var dev_control = 'rid-device';
        }else{
            var dev = '禁封设备';
            var dev_color = 'btn-danger';
            var dev_control = 'ban-device';
        }

        if(row.u_id !== row.old_uid)
            var reset_id = 'inline-block';

        else
            var reset_id = 'none';

        return [
            '<button type="button"  class="userInfo-btn btn btn-warning  btn-sm" style="margin-right:15px;">查看用户信息</button>',
            '<button type="button"  class="'+control +' btn '+color+'  btn-sm" style="margin-right:15px;">'+state+'</button>',
           // '<button type="button"  class="build-union btn btn-primary  btn-sm" style="margin-right:15px; display:'+sh+';">开通工会</button>',
            '<button type="button" id="reset-password" class="reset-password btn-danger  btn-sm" style="margin-right:15px;">重设密码</button>',
            '<button type="button" id="ban-device" class=" '+dev_control+' btn '+dev_color+' btn-sm" style="margin-right:15px;">'+dev+'</button>',
            '<button type="button" id="ban-device" class="rid-lock btn btn-primary btn-sm" style="margin-right:15px;">解封宝箱</button>',
            '<button type="button" id="reset_id" class="reset-id btn btn-warning btn-sm" style="margin-right:15px; display: '+reset_id +'">还原初始ID</button>',
            '<button type="button"  class="diamonds-source btn btn-warning btn-sm" style="margin-right:15px;">球币明细</button>'
        ].join('');


    }

    window.operateEvents = {
        'click .userInfo-btn':function(e,value,row,index){
            layer.open({
                type: 2,
                title: ['用户信息','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserInfo?user_id=" + row.id,
                offset:'auto',
                scrollbar:false
            });
        },

        'click .ban-btn':function(e,value,row,index){
            layer.open({
                type: 2,
                title: ['禁封账号','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserBan?user_id=" + row.id,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });
        },

        'click .rid-btn':function(e,value,row,index){
            layer.confirm('确定解封吗？',{
                title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定','取消'] //按钮
            },function(){
                $.ajax({
                    type:'get',
                    url:"/UserRid?user_id=" + row.id,
                    async:true,
                    dataType:'json',
                    success:function(result){
                        if(result.code == 100){
                            layer.msg(result.msg,{
                                icon:1,
                                time:1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else{
                            layer.msg(result.msg,{
                                icon:2,
                                time:1000
                            });
                        }
                    },
                    error:function(err){
                        layer.msg('服务器开小差了！',{
                            time:1500
                        });
                    }
                });
            });
        },

        'click .build-union':function(e,value,row,index){
            layer.confirm('确定要开通家族吗？',{
                title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定','取消'] //按钮
            },function(){
                $.ajax({
                    type:'get',
                    url:"/UserBuildFamily?user_id=" + row.id,
                    async:true,
                    dataType:'json',
                    success:function(result){
                        if(result.code == 100){
                            layer.msg(result.msg,{
                                icon:1,
                                time:1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else{
                            layer.msg(result.msg,{
                                icon:2,
                                time:1000
                            });
                        }
                    },
                    error:function(err){
                        layer.msg('服务器开小差了！',{
                            time:1500
                        });
                    }
                });
            });
        },

        'click .agree-union':function(e,value,row,index){

            layer.open({
                type: 2,
                title: ['预审家族','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserBuildAgreeFamily?user_id=" + row.id,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });
        },
        'click .reset-password':function(e,value,row,index){
            layer.open({
                type: 2,
                title: ['重设密码','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserResetPassword?user_id=" + row.id,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });
        },
        'click .rid-lock': function (e, value, row, index) {
            $.ajax({
                type: 'get',
                url: "/RidUserLock?user_id=" + row.id,
                async: true,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 100) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        });
                        $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                    }
                    else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                },
                error: function (err) {
                    layer.msg('服务器开小差了！', {
                        time: 1500
                    });
                }
            })
        },
        'click .ban-device': function (e, value, row, index) {
            layer.confirm('确定要禁封该用户的设备吗？', {
                title: ['提示', 'font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'get',
                    url: "/BanUserDevice?user_id=" + row.id,
                    async: true,
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 100) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    },
                    error: function (err) {
                        layer.msg('服务器开小差了！', {
                            time: 1500
                        });
                    }
                });
            });
        },
        'click .rid-device': function (e, value, row, index) {
            layer.confirm('确定要解封该用户的设备吗？', {
                title: ['提示', 'font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'get',
                    url: "/RidUserDevice?user_id=" + row.id,
                    async: true,
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 100) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    },
                    error: function (err) {
                        layer.msg('服务器开小差了！', {
                            time: 1500
                        });
                    }
                });

            });
        },
        'click .reset-id': function (e, value, row, index) {
            layer.confirm('确定要重置为原始ID吗？', {
                title: ['提示', 'font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'get',
                    url: "/ResetUserId?user_id=" + row.id,
                    async: true,
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 100) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                        }
                        else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    },
                    error: function (err) {
                        layer.msg('服务器开小差了！', {
                            time: 1500
                        });
                    }
                });

            });
        },
        'click .diamonds-source': function (e, value, row, index) {
            layer.open({
                type: 2,
                title: ['球币明细','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content:"/UserDiamondsSource?user_id=" + row.id,
                offset:'auto',
                scrollbar:false
            });
        }
    };
    // 搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

    });
</script>
</body>
</html>